<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <el-button slot="after" type="primary" @click="addPermission(1, '0')">
          添加权限
        </el-button>
      </page-tools>
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column
          label="标识"
          align="center"
          prop="code"
        ></el-table-column>
        <el-table-column
          label="描述"
          align="center"
          prop="description"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="addPermission(2, row.id)"
            >
              添加
            </el-button>
            <el-button type="text" @click="editPermision(row.id)">
              编辑
            </el-button>
            <el-button type="text" @click="delPermission(row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增 编辑权限的弹层-->
      <el-dialog :title="title" :visible="showDialog" @close="cancel">
        <el-form
          ref="form"
          label-width="120px"
          :model="formData"
          :rules="rules"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="formData.name" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="标识" prop="code">
            <el-input v-model="formData.code" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="描述">
            <el-input
              v-model="formData.description"
              style="width: 90%"
            ></el-input>
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              inactive-value="0"
              active-value="1"
            ></el-switch>
          </el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="6">
              <el-button type="primary" size="small" @click="submit">
                确定
              </el-button>
              <el-button size="small" @click="cancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getPermissionList,
  delPermission,
  addPermission,
  getPermissionDetail,
  updatePermission
} from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [],
      formData: {
        name: '',
        code: '',
        type: '',
        pid: '',
        description: '',
        enVisible: '0'
      },
      showDialog: false,
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = tranListToTreeData(await getPermissionList(), '0')
      console.log(this.list)
    },
    delPermission(id) {
      this.$confirm('确定删除该权限？')
        .then(() => {
          return delPermission(id)
        })
        .then(() => {
          this.$message.success('删除权限成功')
          this.getPermissionList()
        })
    },
    addPermission(type, pid) {
      // 访问权 - type=1    操作权 - type=2
      // pid 代表当前数据的父节点的标识
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
      console.log(pid)
    },
    submit() {
      this.$refs.form
        .validate()
        .then(() => {
          if (this.formData.id) {
            return updatePermission(this.formData)
          }
          return addPermission(this.formData)
        })
        .then(() => {
          this.$message.success('操作成功')
          this.getPermissionList()
          this.showDialog = false
        })
        .catch((error) => {
          console.log(error)
        })
    },
    cancel() {
      this.formData = {
        name: '',
        code: '',
        type: '',
        pid: '',
        description: '',
        enVisible: '0'
      }
      this.showDialog = false
      this.$refs.form.resetFields()
    },
    async editPermision(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    }
  }
}
</script>

<style></style>
